package com.ziki.test.socket.util;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;

public class PubFun {
    private static int seq = 1;

    public static String convertBytesToString(byte bstr[]) {
        StringBuffer sb = new StringBuffer();
        try {
            for (int i = 0; i < bstr.length; i++) {
                if (Integer.toHexString(bstr[i] & 0xff).length() == 1) {
                    sb.append("0");
                }
                sb.append(Integer.toHexString(bstr[i] & 0xff)); // 转换成16进制的string格式
            }
        } catch (Exception e) {
        }
        return sb.toString();
    }

    public static byte[] convertStringToBytes(String srcBuff) {
        byte des[];
        try {
            des = new byte[srcBuff.length() / 2];
            int j = 0;
            for (int i = 0; i < srcBuff.length();) {
                String subBuff = srcBuff.substring(i, i + 2);
                byte ab = (byte) Integer.parseInt(subBuff, 16);
                des[j] = ab;
                i += 2;
                j++;
            }
        } catch (Exception e) {
            des = null;
        }
        return des;
    }

    // 将str加入data数组中，数组长度为StrLen，如果不足，右边补0，count为起始位数
    /**
     * 将str加入data数组中，数组长度为StrLen，如果不足，右边补0，start为起始位数
     * 
     * @param str
     * @param data
     * @param len
     * @param start
     * @return
     */
    public static boolean writeString(String str, byte data[], int len, int start) {
        try {
            for (int i = 0; i < str.length(); i++) {
                data[start++] = (byte) str.charAt(i);
            }
            for (int i = str.length(); i < len; i++) {
                data[start++] = 0;
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public static boolean writeIntToBytes(DataOutputStream outStream, String str) {
        byte strtoBytes[] = new byte[4];
        try {
            strtoBytes = str.getBytes();
            outStream.write(strtoBytes);
        } catch (Exception ex) {
            return false;
        }
        return true;
    }

    // 将int类型转换成4字节数组
    /**
     * 将int类型转换成4字节数组
     * 
     * @param number
     * @return
     */
    public static byte[] toByteArray(int number) {
        int temp = number;
        byte b[] = new byte[4];
        for (int i = b.length - 1; i > -1; i--) {
            b[i] = (new Integer(temp & 0xff)).byteValue();
            temp >>>= 8;
        }
        return b;
    }

    // 将int类型转换成N字节数组
    /**
     * 将int类型转换成N字节数组
     * 
     * @param number
     * @param byteNumber
     * @return
     */
    public static byte[] toByteArray(long number, int byteNumber) {
        long temp = number;
        byte b[] = new byte[byteNumber];
        for (int i = b.length - 1; i > -1; i--) {
            b[i] = (new Long(temp & 0xff)).byteValue();
            temp >>>= 8;
        }
        return b;
    }

    public static String getSystemTime() {
        String sysTimeStr = " ";
        Calendar now = Calendar.getInstance();
        String yyyy = String.valueOf(now.get(1));
        String mm = String.valueOf(now.get(2) + 1);
        String dd = String.valueOf(now.get(5));
        String hh = String.valueOf(now.get(11));
        String ff = String.valueOf(now.get(12));
        String ss = String.valueOf(now.get(13));
        mm = 1 != mm.length() ? mm : "0" + mm;
        dd = 1 != dd.length() ? dd : "0" + dd;
        hh = 1 != hh.length() ? hh : "0" + hh;
        ff = 1 != ff.length() ? ff : "0" + ff;
        ss = 1 != ss.length() ? ss : "0" + ss;
        try {
            sysTimeStr = mm + dd + hh + ff + ss;
        } catch (Exception e) {
        }
        return sysTimeStr;
    }

    public static String add86(String mobile) {
        // if (mobile.substring(0, 2).equals("13")) {
        mobile = "86" + mobile;
        // }
        return mobile;
    }

    public static String del86(String mobile) {
        if (mobile.substring(0, 2).equals("86")) {
            mobile = mobile.substring(2);
        }
        return mobile;
    }

    public static String readString(DataInputStream dis, int len) {
        byte readByte[];
        if (len <= 0 || len >= 2101) {
            return "";
        }
        try {
            readByte = new byte[len];
            dis.readFully(readByte);
        } catch (IOException e) {
            return "error!!!";
        }
        String result = new String(readByte, 0, 0, len);
        return result.trim();
    }

    /**
     * 生成发送包的序列号
     * 
     * @return
     */
    public static int getSeq() {
        if (seq > 9999999) {
            seq = 1;
        }
        else {
            seq++;
        }
        return seq;
    }

    public static String nullToEmpty(String strvalue) {
        if (strvalue == null) {
            return "";
        }
        else {
            return strvalue.trim();
        }
    }

    public static byte[] deCode(String src) { // 解码
        if (src.length() < 2) {
            return new byte[0];
        }
        byte[] dest = new byte[src.length() / 2];
        byte rb;
        String str;
        Arrays.fill(dest, (byte) 0);
        int index = 0;
        for (int ii = 0; ii < src.length() - 1; ii++) {
            str = "#" + src.substring(ii, ii + 2);
            rb = (byte) Integer.decode(str).intValue();
            dest[index++] = rb;
            ii++;
        }
        return dest;
    }

    public static String[] getStringEnd(byte[] data, int start, int maxLen) {
        String[] ret = new String[2];
        String str = "";
        String loc = "";
        int len = maxLen;
        if (data.length < start + maxLen) {
            len = data.length - start;
        }
        for (int i = 0; i < len; i++) {
            if (data[start + i] == 0) {
                len = i;
            }
        }
        byte[] newData = new byte[len];
        System.arraycopy(data, start, newData, 0, newData.length);
        str = new String(newData);
        loc = Maths.itoa(start + len + 1);
        ret[0] = str;
        ret[1] = loc;
        return ret;
    }

    public static byte[] getByteEnd(byte[] data, int start, int maxLen) {
        int len = maxLen;
        if (data.length < start + maxLen) {
            len = data.length - start;
        }
        for (int i = 0; i < len; i++) {
            if (data[start + i] == 0) {
                len = i;
            }
        }
        byte[] newData = new byte[len + 1];
        System.arraycopy(data, start, newData, 0, newData.length);
        return newData;
    }

    public static String getNowTimeStr() {
        String ret = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ret = sdf.format(new java.util.Date());
        return ret;
    }

    public static String strnull(String str) {
        if (str == null) {
            return "";
        }
        else {
            return str;
        }
    }

    /**
     * 将byte[]转换成int
     * 
     * @param b
     * @return
     */
    public static int bytes2int(byte[] b) {
        // byte[] b=new byte[]{1,2,3,4};
        int mask = 0xff;
        int temp = 0;
        int res = 0;
        for (int i = 0; i < 4; i++) {
            res <<= 8;
            temp = b[i] & mask;
            res |= temp;
        }
        return res;
    }

    public static byte[] string2byte(String str) {
        byte[] data = new byte[str.length() + 1];
        for (int i = 0; i < str.length(); i++) {
            data[i] = (byte) str.charAt(i);
        }
        data[str.length()] = 0;// 以null结束
        return data;
    }

    public static String padLeading(String str, int len, String addValue) {
        String lTmpPad = "";
        str = strnull(str);
        if (str.length() >= len) {
            return str.substring(0, str.length());
        }
        else {
            for (int i = 1; i <= len - str.length(); i++) {
                lTmpPad = lTmpPad + addValue;
            }
        }
        return lTmpPad + str;
    }
}
